<template>
  <div class="app-container" style="padding: 10px">
    <el-card class="box-card" shadow="false">
      <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="80px">
        <el-form-item label="试卷名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入试卷名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="标签" prop="tag">
          <el-select v-model="queryParams.tags" placeholder="请选择类型" clearable filterable>
            <el-option v-for="dict in tags"
                       :key="dict.id"
                       :label="dict.tagName"
                       :value="dict.id"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery"> 搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery"> 重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="small"
            @click="handleAdd"
          >
            新建试卷
          </el-button>
        </el-col>
        <right-toolbar :show-search.sync="showSearch" @queryTable="getList"/>
      </el-row>

      <el-table v-loading="loading" :data="dataList" border>
        <el-table-column label="ID" prop="id" width="200px" v-if="false"/>
        <el-table-column label="试卷名称" prop="name">
          <template slot-scope="scope">
            《{{ scope.row.name }}》
          </template>
        </el-table-column>
        <el-table-column label="试卷分数" prop="name">
          <template slot-scope="scope">
            {{ scope.row.fraction }}分
          </template>
        </el-table-column>
        <el-table-column label="及格分数" prop="name">
          <template slot-scope="scope">
            {{ scope.row.passFraction.toString() }}分
          </template>
        </el-table-column>
        <el-table-column label="标签" prop="name">
          <template slot-scope="scope">
            <el-tag style="margin: 0 5px" size="mini" v-for="(tag, index) in scope.row.tagList" :key="index">{{ tag }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" width="150">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.exam_test_paper_status" :value="scope.row.status" size="mini"/>
          </template>
        </el-table-column>
        <el-table-column label="创建人" prop="createBy" align="center" width="150"/>
        <el-table-column label="创建时间" prop="createTime" align="center" width="180"/>
        <el-table-column label="操作" align="center" width="180">
          <template slot-scope="scope">
            <el-button
              icon="el-icon-view"
              size="mini"
              type="text"
              @click="handlePreview(scope.row.id)">
              预览
            </el-button>
            <el-button
              icon="el-icon-edit"
              size="mini"
              type="text"
              :disabled="scope.row.status === '1'"
              @click="handleUpdate(scope.row.id)"
            >
              修改
            </el-button>
            <el-button
              icon="el-icon-delete"
              size="mini"
              type="text"
              :disabled="scope.row.status === '1'"
              @click="handleDelete(scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :limit.sync="queryParams.pageSize"
        :page.sync="queryParams.pageNum"
        :total="total"
        @pagination="getList"
      />

      <el-drawer :visible.sync="previewShow" title="试卷预览" v-if="previewShow">
        <preview-test-paper :id="previewId"/>
      </el-drawer>
    </el-card>
  </div>
</template>

<script>
import {testPaperDelete, testPaperList} from "@/api/exam/testPaper";
import previewTestPaper from "./preview.vue"
import {tagList} from "@/api/edu/trainPlan";

export default {
  name: 'TestPaper',
  dicts: ['exam_test_paper_status'],
  components: {previewTestPaper},
  data() {
    return {
      loading: false,
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      dataList: [],
      previewId: null,
      previewShow: false,
      tags: []
    }
  },
  created() {
    this.getTags()
    this.handleQuery()
  },
  methods: {
    getTags() {
      tagList().then(res => {
        this.tags = res.data
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10
      }
      this.getList()
    },
    getList() {
      this.loading = true
      testPaperList(this.queryParams).then(res => {
        this.dataList = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    handlePreview(id) {
      this.previewId = id
      this.previewShow = true
    },
    handleUpdate(id) {
      this.$router.push({
        path: `/exam/testPaper/option`,
        query: {
          id
        }
      })
    },
    handleDelete(id) {
      this.$confirm('是否删除该试卷?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        testPaperDelete(id).then(res => {
          this.handleQuery()
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        })
      })
    },
    handleAdd() {
      this.$router.push({
        path: "/exam/testPaper/option"
      })
    }
  }
}
</script>

<style scoped>

</style>
